package com.project.web.system.controller.app;

import com.github.pagehelper.PageInfo;
import com.project.common.core.utils.exception.Result;
import com.project.web.system.controller.common.BaseController;
import com.project.web.system.model.app.AdminRole;
import com.project.web.system.model.app.Menu;
import com.project.web.system.model.app.Admin;
import com.project.web.system.service.app.AdminRoleService;
import com.project.web.system.service.app.MenuService;
import com.project.web.system.service.app.AdminService;
import com.project.web.util.PageInfoUtil;
import com.project.web.util.exception.SYSTEM_RESP_CODE_ENUM;
import com.project.web.util.exception.SystemCustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户信息API
 *
 * @Author: wyy
 * @Date: 2020-04-11 17:27
 */
@Api(tags = "【Web】系统用户信息API")
@RestController("systemSysUserInfoController")
@RequestMapping("/system/auth/sysUser")
public class AdminController extends BaseController {

    // 日志工具类
    public static final Logger log = LoggerFactory.getLogger(AdminController.class);

    @Resource(name = "sysUserServiceImpl")
    private AdminService sysUserService;

    @Autowired
    private AdminRoleService adminRoleService;

    @Autowired
    private MenuService menuService;

    /**
     * 查询系统用户
     *
     * @param sysUser 系统用户
     * @return
     */
    @ApiOperation(value = "查询系统用户列表【含分页】")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", dataType = "Integer", required = true),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页条数", dataType = "Integer", required = true),
            @ApiImplicitParam(paramType = "query", name = "sysUser", value = "章节信息", dataType = "SysUser", required = true)
    })
    @GetMapping(value = "/getPageList")
    public Result<PageInfo<Admin>> getPageList(Admin sysUser, Integer pageNum, Integer pageSize) {
        PageInfo pageInfo = PageInfoUtil.initPageInfo(pageNum, pageSize);
        PageInfo<Admin> sysUserPageInfo = sysUserService.selectList(sysUser, pageInfo);
        return getResult(sysUserPageInfo);
    }

    /**
     * 查询老师列表
     *
     * @return
     */
    @ApiOperation(value = "查询老师列表")
    @GetMapping(value = "/getTeachers")
    public Result<List<Admin>> getTeachers() {
        List<Admin> sysUserList = sysUserService.getTeachers();
        return getResult(sysUserList);
    }


    /**
     * 获取用户详情
     *
     * @return
     */
    @ApiOperation(value = "获取用户详情")
    @GetMapping(value = "/detail")
    public Result detail(Long userId) {
        if (userId == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        Admin sysUser = sysUserService.getById(userId);

        AdminRole adminRole = new AdminRole();
        adminRole.setUserId(userId);
        List<AdminRole> adminRoles = adminRoleService.selectList(adminRole);
        if (CollectionUtils.isNotEmpty(adminRoles)) {
            List<Long> roles = new ArrayList<>();
            for (AdminRole role : adminRoles) {
                roles.add(role.getRoleId());
            }
            sysUser.setRoleIds(roles);
        }
        return getResult(sysUser);
    }

    /**
     * 修改系统用户信息
     *
     * @return
     */
    @ApiOperation(value = "修改系统用户信息")
    @PostMapping(value = "/updateSysUser")
    public Result updateSysUser(@RequestBody Admin sysUser) {
        sysUserService.update(sysUser);
        return getResult(null);
    }

    /**
     * 添加系统用户信息
     *
     * @return
     */
    @ApiOperation(value = "添加系统用户信息")
    @PostMapping(value = "/saveSysUser")
    public Result saveSysUser(@RequestBody Admin sysUser) {
        sysUserService.save(sysUser);
        return getResult("添加成功");
    }


    /**
     * @param lists
     * @return
     */
    @ApiOperation(value = "系统用户赋予角色")
    @PostMapping(value = "/givenRole")
    public Result givenRole(List<AdminRole> lists) {
        if (CollectionUtils.isEmpty(lists)) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        //根据用户ID删除角色
        AdminRole adminRole = lists.get(0);
        adminRoleService.deteleByAdminId(adminRole.getUserId());

        return getResult(null);
    }

    /**
     * @param userId
     * @return
     */
    @ApiOperation(value = "查看菜单类目")
    @GetMapping(value = "/selectMenu")
    public Result selectMenu(Long userId) {
        if (userId == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        List<Menu> menus = menuService.selectMenu(userId);
        return getResult(menus);
    }


    /**
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询按钮权限")
    @GetMapping(value = "/selectBottonMenu")
    public Result selectBottonMenu(Long userId) {
        if (userId == null) {
            throw new SystemCustomException(SYSTEM_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        return getResult(menuService.selectButtonMenu(userId));
    }


    /**
     * 查询销售列表
     *
     * @return
     */
    @ApiOperation(value = "查询销售列表")
    @GetMapping(value = "/getScList")
    public Result<List<Admin>> getScList() {
        Admin sysUser = new Admin();
        sysUser.setStatus(true);
        sysUser.setDeptId(9);
        List<Admin> sysUserList = sysUserService.selectList(sysUser);
        return getResult(sysUserList);
    }

}
